import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../layout/Layout.vue'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: Layout,
    meta: {
      title: '首页',
      requiresAuth: true
    },
    children: [
      {
        path: '',
        name: 'DashboardHome',
        component: () => import('../views/Dashboard.vue'),
        meta: {
          title: '首页',
          requiresAuth: true
        }
      },
      // 产品管理
      {
        path: 'product/list',
        name: 'ProductList',
        component: () => import('../views/product/ProductList.vue'),
        meta: {
          title: '产品清单',
          requiresAuth: true
        }
      },
      {
        path: 'product/members',
        name: 'ProductMemberManager',
        component: () => import('../views/product/ProductMemberManager.vue'),
        meta: {
          title: '产品人员管理',
          requiresAuth: true
        }
      },
      {
        path: 'product/requirements',
        name: 'ProductRequirement',
        component: () => import('../views/product/ProductRequirement.vue'),
        meta: {
          title: '产品需求管理',
          requiresAuth: true
        }
      },
      {
        path: 'product/test-versions',
        name: 'TestVersionManager',
        component: () => import('../views/product/TestVersionManager.vue'),
        meta: {
          title: '测试版本管理',
          requiresAuth: true
        }
      },
      // 测试申请管理
      {
        path: 'application/list',
        name: 'ApplicationList',
        component: () => import('../views/application/ApplicationList.vue'),
        meta: {
          title: '测试申请单列表',
          requiresAuth: true
        }
      },
      {
        path: 'application/create',
        name: 'CreateApplication',
        component: () => import('../views/application/CreateApplication.vue'),
        meta: {
          title: '创建测试申请',
          requiresAuth: true
        }
      },
      {
        path: 'application/approval',
        name: 'Approval',
        component: () => import('../views/application/Approval.vue'),
        meta: {
          title: '审批待办',
          requiresAuth: true
        }
      },
      // 质量分析
      {
        path: 'quality/analysis',
        name: 'QualityAnalysis',
        component: () => import('../views/quality/QualityAnalysis.vue'),
        meta: {
          title: '质量分析报告',
          requiresAuth: true
        }
      },
      // 消息中心
      {
        path: 'message/center',
        name: 'MessageCenter',
        component: () => import('../views/message/MessageCenter.vue'),
        meta: {
          title: '消息中心',
          requiresAuth: true
        }
      },
      // 系统管理
      {
        path: 'system/users',
        name: 'UserList',
        component: () => import('../views/system/UserList.vue'),
        meta: {
          title: '用户管理',
          requiresAuth: true,
          roles: ['ADMIN']
        }
      },
      {
        path: 'system/roles',
        name: 'RoleList',
        component: () => import('../views/system/RoleList.vue'),
        meta: {
          title: '角色管理',
          requiresAuth: true,
          roles: ['ADMIN']
        }
      },
      {
        path: 'system/menus',
        name: 'MenuList',
        component: () => import('../views/system/MenuList.vue'),
        meta: {
          title: '菜单管理',
          requiresAuth: true,
          roles: ['ADMIN']
        }
      }
    ]
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title + ' - 预集成测试管理平台'
  }

  // 判断是否需要登录
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    const userInfo = localStorage.getItem('userInfo')
    
    if (!token || !userInfo) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }

    // 检查角色权限
    const parsedUserInfo = JSON.parse(userInfo)
    // 使用roles数组代替role属性，确保与localStorage中存储的格式一致
    const userRoles = parsedUserInfo.roles || []
    
    if (to.meta.roles && !to.meta.roles.some(role => userRoles.includes(role))) {
      // 没有权限，跳转到首页
      next('/dashboard')
      return
    }
  }

  next()
})

export default router